package tom.client;

import java.util.List;

import tom.shared.LookupRecord;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.DoubleClickEvent;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.ToggleButton;
import com.google.gwt.user.client.ui.CheckBox;

public class LookupDialog extends Composite {

	private static final int RESULT_LIMITED = 500;

	private static DialogBox dlg;

	private static LookupDialog lookup;

	private static String fname;

	private static TextBox outputCode;

	private static TextBox outputText;

	private static String lastCode = null;

	public static void show(String title, String fname, TextBox outputCode,
			TextBox outputText) {

		LookupDialog.fname = fname;
		LookupDialog.outputCode = outputCode;
		LookupDialog.outputText = outputText;
		if (dlg == null) {
			lookup = new LookupDialog();
			dlg = GWT.create(DialogBox.class);
			dlg.setWidget(lookup);
			dlg.setModal(true);
		}
		dlg.setText(title);
		dlg.center();
		//if (!fname.equals(lastCode)) {
			lookup.textSearch.setText(null);
		//}
		lastCode = fname;
		lookup.textSearch.setFocus(true);
		lookup.onBtnSearchClick(null);
	}

	public static void show(String title, String fname, TextBox output) {
		show(title, fname, output, null);
	}

	private static LookupDialogUiBinder uiBinder = GWT
			.create(LookupDialogUiBinder.class);

	@UiField
	AbsolutePanel aPanel;
	@UiField
	Button btnSearch;
	@UiField
	TextBox textSearch;
	@UiField
	Button btnOK;
	@UiField
	Button btnCancel;
	@UiField
	Button btnClear;
	@UiField
	ListBox listBox;
	@UiField
	Label recordCount;

	List<LookupRecord> list;

	interface LookupDialogUiBinder extends UiBinder<Widget, LookupDialog> {
	}

	public LookupDialog() {
		initWidget(uiBinder.createAndBindUi(this));
		listBox.setDirectionEstimator(false);
	}

	private AsyncCallback<List<LookupRecord>> recordCallback = new AsyncCallback<List<LookupRecord>>() {
		public void onFailure(Throwable caught) {
			btnSearch.setEnabled(true);
			Window.alert(caught.getMessage());
		}

		@Override
		public void onSuccess(List<LookupRecord> result) {
			list = result;
			listBox.setVisible(false);
			listBox.setEnabled(false);
			aPanel.remove(listBox);
			for (int i = 0; i < result.size() && i < RESULT_LIMITED; i++) {
				listBox.addItem(result.get(i).key + " - " + result.get(i).name);
			}
			listBox.setVisible(true);
			listBox.setEnabled(true);
			if (list.size() > 0)
				listBox.setSelectedIndex(0);
			else
				listBox.setSelectedIndex(-1);
			aPanel.add(listBox, 30, 44);
			recordCount.setText(String.valueOf(result.size())
					+ " รายการ"
					+ (result.size() > RESULT_LIMITED ? " แสดงเพียง "
							+ RESULT_LIMITED + " รายการ" : ""));
			btnSearch.setEnabled(true);
			textSearch.setEnabled(true);
//			textSearch.setFocus(true);
			listBox.setFocus(true);
		}
	};

	@UiHandler("btnSearch")
	void onBtnSearchClick(ClickEvent event) {
		listBox.clear();
		recordCount.setText("กำลังดึงข้อมูล...");
		btnSearch.setEnabled(false);
		textSearch.setEnabled(false);
		Library.getService().lookupList(fname, textSearch.getText(),
				recordCallback);
	}

	@UiHandler("btnCancel")
	void onBtnCancelClick(ClickEvent event) {
		outputCode.setFocus(true);
		dlg.hide();
	}

	@UiHandler("btnOK")
	void onBtnOKClick(ClickEvent event) {
		if (listBox.getSelectedIndex() > -1) {
			outputCode.setText(list.get(listBox.getSelectedIndex()).key);
			if (outputText != null)
				outputText.setText(listBox.getItemText(listBox
						.getSelectedIndex()));
		}
		onBtnCancelClick(null);
		outputCode.fireEvent(Library.KEY_ENTER_EVENT);
	}

	@UiHandler("btnClear")
	void onBtnClearClick(ClickEvent event) {
		textSearch.setText(null);
		// onBtnCancelClick(null);
		textSearch.fireEvent(Library.KEY_ENTER_EVENT);
		textSearch.setFocus(true);
	}

	@UiHandler("listBox")
	void onListBoxDoubleClick(DoubleClickEvent event) {
		onBtnOKClick(null);
	}

	@UiHandler("textSearch")
	void onTextSearchKeyDown(KeyDownEvent event) {
		if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER)
			onBtnSearchClick(null);
		if (event.getNativeKeyCode() == KeyCodes.KEY_DOWN)
			listBox.setFocus(true);

	}

	@UiHandler("listBox")
	void onListBoxKeyDown(KeyDownEvent event) {
		if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER)
			onBtnOKClick(null);
	}
}
